<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item
        label="适用业务类"
        prop="BusinessClassID"
        style="width: 240px"
      >
        <el-select v-model="form.BusinessClassID">
          <el-option label="所有业务类" :value="0" />
          <el-option
            v-for="(item, index) in BusinessClassOptions"
            :key="index"
            :label="item.BusinessClassName"
            :value="item.BusinessClassID"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/systemsetBusinessFlowset'
  import { getBusinessClassSelect } from '@/api/common'

  export default defineComponent({
    name: 'SystemsetBusinessFlowsetEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          BusinessClassID: 0,
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
        BusinessClassOptions: [],
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      const getOptions = async () => {
        state.listLoading = true
        const {
          data: { BusinessClassOptions },
        } = await getBusinessClassSelect()
        state.BusinessClassOptions = BusinessClassOptions
        state.listLoading = false
      }

      onMounted(() => {
        getOptions()
      })

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
